package com.yes_iot.dao;

import com.yes_iot.entity.VisualDisplay;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;
@Repository
public interface VisualDisplayDao extends JpaRepository<VisualDisplay, UUID> {
    @Query("select v from VisualDisplay v where v.keycode=:keycode ")
    VisualDisplay findByKeyCode(@Param("keycode") String keycode);
    @Query("select v from VisualDisplay v where v.parentId=:parentId ")
    List<VisualDisplay> findByParentId(@Param("parentId") UUID parentId);
    @Query("select v from VisualDisplay v where v.devCode=:devCode and v.model='1' and v.parentId is null")
    List<VisualDisplay> findByDevId(@Param("devCode")String devCode);
    @Query("select ivdi from VisualDisplay ivdi where ivdi.parentId=:id or ivdi.id=:id order by ivdi.createdTime ")
    List<VisualDisplay> findByParentIdOrId(@Param("id")UUID id);

    @Modifying
    @Transactional
    @Query("update VisualDisplay vd set vd.config=:config , vd.content=:content, vd.loginLogo=:loginLogo, vd.loginText=:loginText where vd.keycode=:keycode")
    void updateByKeycode(@Param("keycode")String keycode, @Param("config")String config, @Param("content")String content ,@Param("loginText")String loginText, @Param("loginLogo")String loginLogo);
    @Modifying
    @Transactional
    @Query("update VisualDisplay vd set vd.name=:name , vd.seq=:seq where vd.keycode=:keycode")
    void updateNameAndSeqByKeycode( @Param("keycode")String keycode, @Param("name") String name,  @Param("seq")Integer seq);

    @Query("select ivdi from VisualDisplay ivdi where ivdi.devModelId=:devModelId and ivdi.parentId is null and ivdi.isMould=true ")
    List<VisualDisplay> findByDevModelId(@Param("devModelId")String devModelId);

    @Modifying
    @Transactional
    @Query("delete from VisualDisplay where parentId=:id ")
    void deleteByParentId(@Param("id")UUID id);
    @Query("select ivdi from VisualDisplay ivdi where ivdi.farmId=:farmId and ivdi.parentId is null and ivdi.model='0' ")
    List<VisualDisplay> findByFarmId(@Param("farmId")Integer farmId);
    @Query("select ivdi from VisualDisplay ivdi where ivdi.devCode in :devCodeList and ivdi.parentId is null and ivdi.model='1' ")
    List<VisualDisplay> findByDevCodeList(@Param("devCodeList")List<String> devCodeList);

    @Query("select ivdi from VisualDisplay ivdi where ivdi.farmId in :farmIdList and ivdi.parentId is null and ivdi.model='0' ")
    List<VisualDisplay> findByFarmIdList(@Param("farmIdList")List<Integer> farmIdList);
    @Query("select case when ivdi.loginText is null then '' else ivdi.loginText end as loginText ,case when ivdi.loginLogo is null then '' else ivdi.loginLogo end as loginLogo from VisualDisplay ivdi where ivdi.keycode= :keyCode ")
    Map<String, Object> findLoginByKeyCode(@Param("keyCode")String keyCode);

}
